package dobest.ray.com.jiongjiong.activity.HomePage.statue.emr;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import dobest.ray.com.jiongjiong.App;
import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.activity.HomePage.HomePagePresenter;
import dobest.ray.com.jiongjiong.adapter.HomePageFragmentPagerAdapter;
import dobest.ray.com.jiongjiong.bean.ArchiveDetailBean;
import dobest.ray.com.jiongjiong.bean.PresenterData;
import dobest.ray.com.jiongjiong.callback.A2FCallBack;
import dobest.ray.com.jiongjiong.callback.PresenterCallBack;
import dobest.ray.com.jiongjiong.constants.Constant;
import dobest.ray.com.jiongjiong.views.NavigationBar;

/**
 * Created by Ray on 2016/4/18.
 * https://github.com/ray0807
 */
public class RecordDetailsActivity extends BaseActivity<HomePagePresenter> implements PresenterCallBack, A2FCallBack<ArchiveDetailBean> {

    private static final String INTENT_TYPE = "INTENT_TYPE";
    private static final String INTENT_ID = "INTENT_ID";
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;

    private View v_1;
    private View v_2;
    private View v_3;
    private View v_4;
    private View v_5;

    private String type;
    private String id;

    private List<A2FCallBack<ArchiveDetailBean>> callbacks = new ArrayList<>();


    public static void startRecordDetailActivity(Context context, String type, String id) {
        Intent intent = new Intent(context, RecordDetailsActivity.class);
        intent.putExtra(INTENT_TYPE, type);
        intent.putExtra(INTENT_ID, id);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_detail);
        findViews();
        init();
        addListeners();
    }

    @Override
    public void findViews() {
        type = getIntent().getStringExtra(INTENT_TYPE);
        id = getIntent().getStringExtra(INTENT_ID);

        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setBackListener(R.mipmap.img_back, "");
        navi.setTitle("视力");
        v_1 = findViewById(R.id.v_1);
        v_2 = findViewById(R.id.v_2);
        v_3 = findViewById(R.id.v_3);
        v_4 = findViewById(R.id.v_4);
        v_5 = findViewById(R.id.v_5);
    }

    @Override
    public void init() {
        setPresenter(new HomePagePresenter(), this);
        InitViewPager();

        loadingDialog.show();
        getPresenter().getArchiveDetail(this, App.getInstance().uid, id, type);
    }

    @Override
    public void addListeners() {

    }

    Fragment farVisonFragment;
    Fragment biologicalEyeFragment;
    Fragment investigationOfEyeDiseaseFragment;
    Fragment refractionFragment;
    Fragment visualFunctionFragment;

    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.vPager);
        mPager.setOffscreenPageLimit(5);
        fragmentsList = new ArrayList<>();

        farVisonFragment = new RecordFarVisonFragment();
        biologicalEyeFragment = new BiologicalEyeFragment();
        investigationOfEyeDiseaseFragment = new InvestigationOfEyeDiseaseFragment();
        refractionFragment = new RefractionFragment();
        visualFunctionFragment = new VisualFunctionFragment();


        fragmentsList.add(farVisonFragment);
        fragmentsList.add(refractionFragment);
        fragmentsList.add(biologicalEyeFragment);
        fragmentsList.add(visualFunctionFragment);
        fragmentsList.add(investigationOfEyeDiseaseFragment);
        callbacks.add((A2FCallBack<ArchiveDetailBean>) farVisonFragment);
        callbacks.add((A2FCallBack<ArchiveDetailBean>) refractionFragment);
        callbacks.add((A2FCallBack<ArchiveDetailBean>) biologicalEyeFragment);
        callbacks.add((A2FCallBack<ArchiveDetailBean>) visualFunctionFragment);
        callbacks.add((A2FCallBack<ArchiveDetailBean>) investigationOfEyeDiseaseFragment);

        mPager.setAdapter(new HomePageFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }


    private ArchiveDetailBean bean;

    @Override
    public <T> void callBackPresenter(PresenterData<T> data) {
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        T t = data.data;
        if (t == null) {
            return;
        }
        if (Constant.ARCHIVE_DETAIL_BEAN_TAG.equals(data.tag)) {
            bean = (ArchiveDetailBean) t;
            if (bean != null) {
                setData(bean);
            }
        }

    }

    /**
     * 设置详细数据
     *
     * @param bean
     */
    private void setData(ArchiveDetailBean bean) {
        if (callbacks.size() > 0) {
            for (A2FCallBack<ArchiveDetailBean> call : callbacks) {
                call.getActivityData(bean);
            }
        }
    }

    @Override
    public void getActivityData(ArchiveDetailBean archiveDetailBean) {

    }


    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    v_1.setBackgroundColor(getResources().getColor(R.color.dangan_choose1));
                    v_2.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_3.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_4.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_5.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    navi.setTitle("视力");
                    break;
                case 1:
                    v_1.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_2.setBackgroundColor(getResources().getColor(R.color.dangan_choose1));
                    v_3.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_4.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_5.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    navi.setTitle("屈光");
                    break;
                case 2:
                    v_1.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_2.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_3.setBackgroundColor(getResources().getColor(R.color.dangan_choose1));
                    v_4.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_5.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    navi.setTitle("眼生物");
                    break;
                case 3:
                    v_1.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_2.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_3.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_4.setBackgroundColor(getResources().getColor(R.color.dangan_choose1));
                    v_5.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    navi.setTitle("视功能");
                    break;
                case 4:
                    v_1.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_2.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_3.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_4.setBackgroundColor(getResources().getColor(R.color.text_real_shallow_gray));
                    v_5.setBackgroundColor(getResources().getColor(R.color.dangan_choose1));
                    navi.setTitle("眼病排查");
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}
